<template>
  <div id="main">
    <el-container>
      <el-aside>
        <side-bar :categories="categories" @category-selected="switchCategory" />
      </el-aside>
      <el-main>
        <el-carousel indicator-position="outside">
          <el-carousel-item v-for="item in carouseData" :key="item">
            <img :src="item.url" class="lunboimg">
          </el-carousel-item>
        </el-carousel>
        <div>
          <el-row :gutter="20">
            <template v-for="i in items">
              <el-col :span="6" :key="i.id">
                <book-item
                    :id="i.id"
                    :title="i.title"
                    :price="i.price"
                    :url="i.imgUrl"/>
              </el-col>
            </template>
          </el-row>
          <el-row>
            <el-col :span="8" :push="8" style="text-align: center">
              <el-button @click="load" :loading="loading" v-if="!noMore">下一页</el-button>
              <p v-else>没有更多了...</p>
            </el-col>
          </el-row>
          <el-backtop :visibility-height="200"></el-backtop>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import BookItem from "@/components/books/BookItem";
import axios from "axios";
import SideBar from "@/components/books/SideBar";

export default {
  name: "MainView",
  components: {SideBar, BookItem},
  data() {
    return {
      noMore: false,
      loading: false,
      items: [],
      page: 0,
      category: "all",  // 现在是category的id
      categories: [],
      carouseData:[
        {url:require("../../public/img/lunbo1.jpg")},
        {url:require("../../public/img/lunbo2.png")},
        {url:require("../../public/img/lunbo3.png")},
      ]
    }
  },
  methods: {
    load() {
      this.loading = true
      axios.get(`/api/book/list${this.queryString()}`)
          .then(res => {
            let data = res.data
            if (data.length < 100) {
              this.noMore = true
            }
            this.items.push(...data)
            this.page++
            this.loading = false
          })
          .catch(e => {
            if (e.response) {
              this.$message({
                message: `未知的服务器错误 代码${e.response.status}`,
                type: "error"
              });
            } else {
              this.$message({
                message: `未知的错误`,
                type: "error"
              });
            }
            this.loading = false
          })
    },
    queryString() {
      let category = this.category !== "all" ? `&category=${this.category}` : "";
      let page = `page=${this.page}`;
      return `?${page}${category}`
    },
    switchCategory(n_val) {
      this.category = n_val
      this.page = 0
      this.items = []
      this.noMore = false
      this.load()
    }
  },
  beforeMount() {
    this.load();
    axios.get("/api/book/fetchCategories")
    .then(res => {
      this.categories = res.data
    })
    .catch(e => {
      if (e.response) {
        this.$message({
          message: `未知的服务器错误 代码${e.response.status}`,
          type: "error"
        });
      } else {
        this.$message({
          message: `未知的错误`,
          type: "error"
        });
      }
    })
  }
}
</script>

<style scoped>
  .lunbo{
    width: 100%;
  }
  .lunboimg{
    width: 100%;
  }
  .el-carousel__item h3 {
    color: #475669;
    font-size: 18px;
    opacity: 0.75;
    line-height: 300px;
    margin: 0;
  }
  
  .el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
  }
  
  .el-carousel__item:nth-child(2n+1) {
    background-color: #d3dce6;
  }
</style>